package main

import (
	"crypto/tls"
	"fmt"
	"log"
	"net/http"
)

func main() {
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintf(w, "Hello, HTTP/2!")
	})

	// 使用 :443 端口启动TLS服务
	server := &http.Server{Addr: ":443"}

	// 加载TLS证书和私钥
	//cert, err := tls.LoadX509KeyPair("./cert.pem", "./private_key.pem")
	cert, err := tls.LoadX509KeyPair("cert.pem", "private_key.pem")
	if err != nil {
		log.Fatalf("Failed to load cert and key: %v", err)
	}

	// 配置TLS
	tlsConfig := &tls.Config{
		Certificates:             []tls.Certificate{cert},
		PreferServerCipherSuites: true,
		MinVersion:               tls.VersionTLS12, // 至少TLS 1.2才能启用HTTP/2
	}

	// 设置服务器的TLS配置
	server.TLSConfig = tlsConfig

	// 启动HTTP/2服务
	log.Println("Starting HTTP/2 server on :443")
	if err := server.ListenAndServeTLS("", ""); err != nil {
		log.Fatalf("Failed to start server: %v", err)
	}
}
